﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2022 kingerrobot
// 版权所有。 
//
// 文件名：BRcsEquipCuring
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2022/1/19 13:52:39
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
using System;

namespace KingerRobot.MexicoSailunWMS.SpiService;
internal class BRcsEquipCuringDitch : IRcsEquipCuringDitch
{

    private readonly static IRcsEquipCuring _RcsEquipCuring = new BRcsEquipCuring();

    public List<CuringTaskCountBean> GetCuringDitchesTask(DateTime BeginDate, DateTime EndDate)
    {
        var db = Db.Default;
        var result = db.RcsEquipCuringDitchs.Join(db.RcsEquipCurings, f => f.Id, s => s.DitchId, (f, s) => new
        {
            CuringId = f.Id,
            f.EquipName,
            s.LocId,
        }).Join(db.BakWcsTasks, f => f.LocId, s => s.TargetLoc, (f, s) => new
        {
            f.CuringId,
            f.EquipName,
            s.CreateTime,
            s.TaskType,
            s.TaskStatus,
            s.UseFlag,
        }).Where(t => t.CreateTime >= BeginDate && t.CreateTime < EndDate && t.TaskType == (int)EnumTaskType.EMS && t.TaskStatus == (int)EnumTaskStatus.Finish && t.UseFlag == 1)
         .GroupBy(t => new { t.EquipName, t.CuringId })
         .Select(s => new CuringTaskCountBean() { CuringName = s.Key.EquipName, CuringId = s.Key.CuringId, TaskCount = s.Count() })
         .OrderBy(t => t.CuringId).ToList();
        return result;
    }

    public List<PORcsEquipCuringDitch> GetRcsEquipCuringDitches()
    {
        var db = Db.Default;
        var result = db.RcsEquipCuringDitchs.ToList();
        return result;
    }

    public int UpdateDicthCount(long id, int count, string reason)
    {
        var db = Db.Default;
        var result = db.RcsEquipCuringDitchs.Where(t => t.Id == id).Update(new PORcsEquipCuringDitch
        {
            MaxCount = count,
            UpdateTime = DateTime.Now,
            Reason = reason
        });
        return 1;
    }

    public int UpdateDitchStop(long id)
    {
        var db = Db.Default;
        var result = 0;
        var data = db.RcsEquipCuringDitchs.Where(t => t.Id == id).FirstOrDefault();
        if (data is not null)
        {
            data.EquipStatus = data.EquipStatus == 2 ? 0 : 2;
            data.UpdateTime = DateTime.Now;
            result = db.SaveChanges();
        }
        if (result > 0)
        {
            _RcsEquipCuring.UpdateCuringStop(id);

        }
        return result;
    }

    public int UpdateDitchSuspend(long id)
    {
        var db = Db.Default;

        var result = 0;
        var data = db.RcsEquipCuringDitchs.Where(t => t.Id == id).FirstOrDefault();
        if (data is not null)
        {
            data.EquipStatus = data.EquipStatus == 1 ? 0 : 1;
            data.UpdateTime = DateTime.Now;
            result = db.SaveChanges();
        }
        if (result > 0)
        {
            _RcsEquipCuring.UpdateCruingSuspend(id);
        }
        return result;
    }
}